﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Net;
using System.Windows;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;


namespace Client
{
    public class Map
    {
        private int size;
        private Dictionary<String, MapField> fields = new Dictionary<String, MapField>();

        public int Size
        {
            get { return this.size; }
            set { this.size = value; }
        }

        public Dictionary<String, MapField> Fields
        {
            get { return fields; }
        }

        public MapField GetField(int index)
        {
            return fields.ElementAt(index).Value;
        }

        public MapField GetField(int x, int y)
        {
            return this.fields[x + "_" + y];
        }

        public Map(Data.Realm.Field[][] fields)
        {
            Random g = new Random();
            size = (int)fields.Length;
            Random generator = new Random();

            for (int i = 0; i < fields.Length; i++)
            {
                for (int j = 0; j < fields.Length; j++)
                {
                    Color type = MapField.TYPE_DEFAULT;
                    fields[i][j].status = g.Next(0,3); ;
                    AddField(i, j, fields[i][j].status, fields[i][j].plant_name, fields[i][j].drive_cost);
                }
            }
        }

        public void Clear()
        {
            foreach (KeyValuePair<string, MapField> field in this.fields)
            {
                field.Value.Panel.Hide();
            }
            this.fields.Clear();
            GC.Collect();
        }

        public void clearMap()
        {
            int size_field = (Tractor.MainForm.AgentPanel.Height - 20) / Tractor.Map.Size;
            foreach (KeyValuePair<string, MapField> field in this.fields.ToList())
            {
                lock (field.Value.Panel)
                {                 
                    System.Drawing.Graphics GraphicsObject = field.Value.Panel.CreateGraphics();
                    GraphicsObject.Clear(field.Value.Color);
                }
            }
        }

        public void RenderMap()
        {
            int size_field = (Tractor.MainForm.AgentPanel.Height - 20) / Tractor.Map.Size;
            foreach (KeyValuePair<string, MapField> field in this.fields.ToList())
            {
                lock (field.Value.Panel)
                {
                    field.Value.Panel.BackColor = field.Value.Color;
                    field.Value.Panel.Height = size_field;
                    field.Value.Panel.Width = size_field;
                    field.Value.Panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    System.Drawing.Point location = new System.Drawing.Point(((field.Value.Panel.Height - 1) * field.Value.CoordianteX) + 10, ((field.Value.Panel.Width - 1) * (Tractor.Map.Size - field.Value.CoordianteY - 1)) + 10);
                    field.Value.Panel.Location = location;
                    System.Drawing.Graphics GraphicsObject = field.Value.Panel.CreateGraphics();
                    GraphicsObject.Clear(field.Value.Color);
                    if (Tractor.Position.Key == field.Value.CoordianteX && Tractor.Position.Value == field.Value.CoordianteY)
                    {
                        GraphicsObject.DrawImage(Tractor.Image, 0, 0, size_field, size_field);
                        GraphicsObject.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    }
                    System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
                    ToolTip1.SetToolTip(field.Value.Panel, "Pozycja: " + (field.Value.CoordianteX + 1) + 'x' + (field.Value.CoordianteY + 1) + Environment.NewLine 
                        + "Roślina: " + (field.Value.Plant == null ? "brak" : field.Value.Plant) + Environment.NewLine
                        + "Koszt przejazdu: " + field.Value.DriveCost + " zł");
                }
            }
        }

        public void RenderTractor(int FX, int FY)
        {
            int size_field = (Tractor.MainForm.AgentPanel.Height - 20) / Tractor.Map.Size;
            foreach (KeyValuePair<string, MapField> field in this.fields.ToList())
            {
                lock (field.Value.Panel)
                {
                    System.Drawing.Graphics GraphicsObject = field.Value.Panel.CreateGraphics();
                    if (FX == field.Value.CoordianteX && FY == field.Value.CoordianteY)
                    {
                        GraphicsObject.DrawImage(Tractor.Image, 0, 0, size_field, size_field);
                        GraphicsObject.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    }
                }
            }
        }

        public void PrintSolution(ArrayList ASolution)
        {
            for (int j = 0; j < 10; j++)
            {
                for (int i = 0; i < 10; i++)
                {
                    AStarNode2D tmp = null;
                    bool solution = false;
                    foreach (AStarNode2D n in ASolution)
                    {
                        tmp = new AStarNode2D(null, null, 0, j, i);
                        solution = n.IsSameState(tmp);
                        if (solution)
                            break;
                    }
                    if (solution)
                    {
                        this.RenderTractor(j, i);
                        MapField field = Tractor.Map.GetField(j,i);

                        string row =  "Byłem na polu x: " + (field.CoordianteX + 1) + " i y: " + (field.CoordianteY+1) + " kosztowało mnie :" + field.DriveCost;
                        var listViewItem = new ListViewItem(row);
                        Tractor.MainForm.listView1.Items.Add(listViewItem);
                    }
                    else
                        if (i == -1)
                            //Console.Write("X ");
                            continue;
                        else
                            //Console.Write(". ");
                            continue;
                }
            }
        }

        public void AddField(int x, int y, int field_typ, string plant, int drive_cost)
        {
            this.fields.Add(x + "_" + y, new MapField(x, y, field_typ, plant, drive_cost));
        }

        public void AddStartField(int field_typ, string plant, int drive_cost)
        {
            this.fields.Add("0_0", new MapField(0, 0, field_typ, plant, drive_cost));
        }

    }
}
